import pandas as pd
import os


EXCLUDE_FILES = ['通行设施.csv', '室内设施.csv', '地名地址信息.csv', '事件活动.csv']
LINES_CHUNK = 10000

def _sovle(tt):
    try:
        if isinstance(tt, str):
            return tt.replace("|", "A")
    except BaseException as e:
        print(type(tt))
        print(tt)
        raise e


def get_all_csv(source_path, dest_path):
    res = []
    for fileName in os.listdir(source_path):
        source_file_path = source_path+os.sep+fileName
        if fileName.endswith(".csv") and fileName not in EXCLUDE_FILES:
            res.append(
                (source_file_path, dest_path+os.sep +
                 os.path.splitext(os.path.split(
                     source_file_path)[-1])[0]+".xlsx"
                 )
            )
        elif os.path.isdir(source_file_path):
            res.extend(get_all_csv(source_path+os.sep +
                       fileName, dest_path+os.sep+fileName))
    return res


files = get_all_csv("csv_type/2017", "xlsx_type/2017")
print(files)
# files = [("csv/2017/风景名胜.csv", "csv_type2/2017/风景名胜.csv")]
# unsolved_files = []
for source_file, dest_file in files:
    print("solving", source_file)
    dest_dir = os.path.dirname(dest_file)
    if not os.path.exists(dest_dir):
        os.makedirs(dest_dir)
    df_tmp = pd.read_csv(source_file)
    df_tmp["TELEPHONE"] = df_tmp["TELEPHONE"].map(lambda tt: _sovle(tt))
    curr_line = 1
    lines = df_tmp.shape[0]
    
    
